package com.lizheng.yunservice.travel.service.impl;

import com.lizheng.yunservice.travel.doamin.VtpScenic;
import com.lizheng.yunservice.travel.doamin.vo.VtpAppScenicListVo;
import com.lizheng.yunservice.travel.doamin.vo.VtpScenicCategoryVo;
import com.lizheng.yunservice.travel.mapper.VtpAppFileResourceMapper;
import com.lizheng.yunservice.travel.mapper.VtpAppScenicHistoryMapper;
import com.lizheng.yunservice.travel.mapper.VtpAppScenicMapper;
import com.lizheng.yunservice.travel.service.VtpAppScenicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class VtpAppScenicServiceImpl implements VtpAppScenicService {

    
    @Autowired
    private VtpAppScenicMapper vtpAppScenicMapper;
    @Autowired
    private VtpAppScenicHistoryMapper vtpAppScenicHistoryMapper;
    @Autowired
    private VtpAppFileResourceMapper vtpAppFileResourceMapper;

    @Autowired
    private VtpAppScenicHistoryMapper scenicHistoryMapper;
    @Autowired
    private VtpAppFileResourceMapper fileResourceMapper;
    @Override
    public List<VtpScenic> findNearbyScenicSpots(Double longitude, Double latitude) {
        List<VtpScenic> spots = vtpAppScenicMapper.selectNearbyScenicSpots(
            longitude, latitude, 50.0
        );
        
        spots.forEach(spot -> {
            spot.setDistance(calculateDistance(
                longitude, latitude,
                Double.parseDouble(spot.getLongitude()),
                Double.parseDouble(spot.getLatitude())
            ));
        });
        
        return spots;
    }

    @Override
    public String findProvince(int ProvinceId) {
        return vtpAppScenicMapper.selectProvince(ProvinceId);
    }

    private double calculateDistance(double lon1, double lat1, double lon2, double lat2) {
        final int R = 6371; // 地球半径(千米)
        
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                   Math.sin(dLon/2) * Math.sin(dLon/2);
        
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        
        return R * c;
    }
    @Override
    public List<VtpScenicCategoryVo> getScenicCategoryList() {
        return vtpAppScenicMapper.getScenicCategoryList();
    }

    @Override
    public List<VtpAppScenicListVo> getScenicList(Long id) {
        // 1. 校验参数
        if (id == null) {
            return Collections.emptyList();
        }

        // 2. 执行查询（假设id为分类ID或其他筛选条件）
        List<VtpAppScenicListVo> result = vtpAppScenicMapper.getScenicList(id);

        // 3. 空值安全处理
        if (result == null) {
            return Collections.emptyList();
        }

        // 4. 处理关联数据
        result.forEach(scenic -> {
            if (Objects.nonNull(scenic.getScenicId())) {
                // 设置历史事件
                scenic.setHistorys(scenicHistoryMapper.selectByScenicId(scenic.getScenicId()));
                // 设置图片集 (type=4)
                scenic.setImgs(fileResourceMapper.selectByScenicIdAndType(scenic.getScenicId(), 4));
                // 设置视频集 (type=5)
                scenic.setVideos(fileResourceMapper.selectByScenicIdAndType(scenic.getScenicId(), 5));
            }
        });

        return result;
    }


}